package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.model.StyleSheets;
import com.conversationboard.model.User;

@WebServlet(name = "ChangeStyleSheetControllerServlet", urlPatterns = "/ChangeStyleSheetControllerServlet")
public class ChangeStyleSheetControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 877143966574896115L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal());

			String fileName = request.getParameter("optionid");

			if (fileName != null) {
				StyleSheets.setStyleSheetFileName(user, fileName, response);
				request.setAttribute("message",
						"Your style has been changed. Your browser may not immediately pick up this change. Please perform a [shift] reload, or refresh your browser's cache to pick up the changes.");
			} else {
				request.setAttribute("message", "Please select a stylesheet from the list.");
			}

			RequestDispatcher messageDispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");
			messageDispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
